export const fakeApi = (name: string): Promise<string[]> =>
  new Promise((resolve, reject) =>
    setTimeout(
      () => resolve(new Array(10).fill(undefined).map((n, i) => `${name}${i}`)),
      1000
    )
  );

const fetchCounties = async (cityName: string) => {
  const counties = await fakeApi(`${cityName} - 县`);
  return counties;
};

const fetchCities = async (provinceName: string) => {
  const cities = await fakeApi(`${provinceName} - 市`);
  return cities;
};

const fetchProvinces = async () => {
  const provinces = await fakeApi(`省`);
  return provinces;
};

const fetch = async () => {
  const result: string[] = [];
  const provinces = await fetchProvinces();
  await Promise.all(
    provinces.map((p) =>
      fetchCities(p).then((cities) =>
        Promise.all(
          cities.map((city) =>
            fetchCounties(city).then((countries) => result.push(...countries))
          )
        )
      )
    )
  );

  console.log(result);
};

fakeApi("test").then(console.log);
fetch();
